package kr.hkit.powerjava.chther23;

class Counter {
	private int value;

	public  void increment() {
		value++;
	}

	public  void decrement() {
		value--;
	}

	public  void printCounter() {
		System.out.println(value);
	}
}

class SharedThread extends Thread {
	Counter sharedCounter;

	public SharedThread(Counter sharCounter) {
		this.sharedCounter = sharCounter;
	}

	@Override
	public void run() {
		int i = 0;
		while (i < 20000) {
			sharedCounter.increment();
			sharedCounter.decrement();
			if (i % 40 == 0) {
				sharedCounter.printCounter();
				try {
					sleep((int) (Math.random() * 2));
				} catch (InterruptedException e) {
				}

			}
			i++;
		}
	}
}

public class CountTest {
	public static void main(String[] args) {
		Counter c = new Counter();
		new SharedThread(c).start();
//		new SharedThread(c);
//		new SharedThread(c);
	}
}
